import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  UseGuards,
  Request,
  Patch,
  ParseIntPipe,
  Query,
} from '@nestjs/common';
import { FavoriteService } from './favorite.service';
import { CreateFavoriteDto } from './dto/create-favorite.dto';
import { ApiOperation, ApiParam, ApiQuery, ApiResponse } from '@nestjs/swagger';
import { UpdateFavoriteDto } from './dto/update-favorite.dto';
import { GetFavoriteVideosDto } from './dto/get-favorite-videos.dto';

@Controller('favorites')
export class FavoriteController {
  constructor(private readonly favoriteService: FavoriteService) {}

  @Post()
  @ApiOperation({ summary: '创建收藏夹' })
  create(@Request() req, @Body() createFavoriteDto: CreateFavoriteDto) {
    return this.favoriteService.createFavorite(req.user.id, createFavoriteDto);
  }

  @Get()
  @ApiOperation({ summary: '查询用户收藏夹列表' })
  findAll(@Request() req) {
    return this.favoriteService.getFavorites(req.user.id);
  }

  @Post(':favoriteId/videos/:videoId')
  @ApiOperation({ summary: '收藏夹新增视频' })
  addVideo(
    @Request() req,
    @Param('favoriteId') favoriteId: number,
    @Param('videoId') videoId: number,
  ) {
    return this.favoriteService.addVideoToFavorite(
      req.user.id,
      favoriteId,
      videoId,
    );
  }

  @Delete(':favoriteId/videos/:videoId')
  @ApiOperation({ summary: '收藏夹删除视频' })
  removeVideo(
    @Request() req,
    @Param('favoriteId') favoriteId: number,
    @Param('videoId') videoId: number,
  ) {
    return this.favoriteService.removeVideoFromFavorite(
      req.user.id,
      favoriteId,
      videoId,
    );
  }

  @Get(':favoriteId/videos')
  @ApiOperation({ summary: '获取收藏夹下的视频' })
  @ApiParam({ name: 'favoriteId', description: '收藏夹ID' })
  @ApiQuery({ name: 'page', description: '页码', required: false, type: Number })
  @ApiQuery({ name: 'limit', description: '每页数量', required: false, type: Number })
  @ApiQuery({ name: 'keyword', description: '搜索关键词', required: false })
  @ApiResponse({ status: 200, description: '获取成功' })
  getFavoriteVideos(
    @Request() req,
    @Param('favoriteId', ParseIntPipe) favoriteId: number,
    @Query() query: GetFavoriteVideosDto,
  ) {
    return this.favoriteService.getFavoriteVideos(req.user.id, favoriteId, query);
  }

  @Delete(':favoriteId')
  @ApiOperation({ summary: '删除收藏夹' })
  remove(@Request() req, @Param('favoriteId') favoriteId: number) {
    return this.favoriteService.deleteFavorite(req.user.id, favoriteId);
  }

  /**
   * @description 更新收藏夹信息
   */
  @Patch(':favoriteId')
  @ApiOperation({ summary: '更新收藏夹信息' })
  @ApiParam({ name: 'favoriteId', description: '收藏夹ID' })
  @ApiResponse({ status: 200, description: '更新成功' })
  updateFavorite(
    @Request() req,
    @Param('favoriteId', ParseIntPipe) favoriteId: number,
    @Body() updateFavoriteDto: UpdateFavoriteDto,
  ) {
    return this.favoriteService.updateFavorite(
      req.user.id,
      favoriteId,
      updateFavoriteDto,
    );
  }
}
